package com.zwz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwz.bean.Contents;
import com.zwz.dao.ContentsDao;
import com.zwz.service.ContentsService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ContentsServiceImpl implements ContentsService {

    @Autowired
    private ContentsDao contentsDao;

    @Override
    public boolean insert(Contents contents) {
        return contentsDao.insert(contents) == 1;
    }

    @Override
    public boolean deleteById(Integer cid) {
        return contentsDao.deleteById(cid) == 1 ;
    }

    @Override
    public boolean updateById(Contents contents) {
        return contentsDao.updateById(contents) == 1;
    }

    @Override
    public Contents getById(Integer cid) {
        return contentsDao.selectById(cid);
    }

    @Override
    public IPage<Contents> getPage(int currentPage, int pageSize, Contents contents) {
        IPage<Contents> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Contents> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(contents.getQuestion()), Contents::getQuestion, contents.getQuestion());
        lqw.eq(Contents::getTid,contents.getTid());
        lqw.orderByDesc(Contents::getCid);
        contentsDao.selectPage(page, lqw);
        return page;
    }

    @Override
    public int countAll() {
        return contentsDao.countAll();
    }

    @Override
    public List<Contents> startList(Integer tid) {
        Map<String,Object> map = new HashMap<>();
        map.put("tid",tid);
        return contentsDao.selectByMap(map);
    }

}
